/// @tags: DPwithConvexHullOption
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

namespace BlueQuantum {

typedef long long LL;

int const N = 1e6 + 5;
LL const INF = 0x3f3f3f3f3f3f3f3f;

int n, m, A, B, C, maxt;
int x[N], y[N], p[N], q[N], l[N], r[N];
LL ans;
LL f[N];
vector<int> vec[N], que[N], ins[N];

inline LL sqr(int x) { return x * x; }

inline double slope(int i, int j) {
  LL dx = q[j] - q[i],
     dy = f[j] + A * sqr(q[j]) - (LL)B * q[j] - (f[i] + A * sqr(q[i]) - (LL)B * q[i]);
  if (!dx) return dy > 0 ? INF : -INF;
  return (double)dy / dx;
}

inline int main() {
  cin >> n >> m >> A >> B >> C;
  for (int i = 1; i <= m; ++i) {
    cin >> x[i] >> y[i] >> p[i] >> q[i];
    vec[p[i]].push_back(i);
    maxt = max(maxt, q[i]);
  }
  ans = INF;
  memset(r, -1, sizeof(r));
  for (int i = 0; i <= maxt; ++i) {
    for (vector<int>::iterator it = ins[i].begin(); it != ins[i].end(); ++it) {
      int j = *it, pos = y[j];
      while (l[pos] < r[pos] && slope(que[pos][r[pos] - 1], que[pos][r[pos]]) >=
                                    slope(que[pos][r[pos] - 1], j))
        r[pos]--;
      ++r[pos] >= int(que[pos].size()) ? que[pos].push_back(j)
                                       : void(que[pos][r[pos]] = j);
    }
    for (vector<int>::iterator it = vec[i].begin(); it != vec[i].end(); ++it) {
      int j = *it, pos = x[j];
      double k = 2.0 * A * i;
      while (l[pos] < r[pos] && slope(que[pos][l[pos]], que[pos][l[pos] + 1]) < k)
        l[pos]++;
      if (l[pos] > r[pos] && x[j] != 1) continue;
      int h = (x[j] == 1 && l[pos] > r[pos]) ? 0 : que[pos][l[pos]];
      f[j] = f[h] + A * sqr(p[j] - q[h]) + (LL)B * (p[j] - q[h]) + C;
      ins[q[j]].push_back(j);
      if (y[j] == n) ans = min(ans, f[j] + q[j]);
    }
  }
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P6302 [NOI2019] 回家路线.in", "r", stdin);
  freopen("P6302 [NOI2019] 回家路线.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
